<?php
//--------------------------------------------------------------------- ARRAYS -

/**
 * Checks if an array is associative or not.
 *
 * Based on Matthias Loitsch comment:
 *   http://www.php.net/manual/en/function.is-array.php#87401
 *
 * @param array $var
 * @return boolean
 * @author Carlos
 */
function is_associative_array($var)
{
  return is_array($var) &&
    (count(array_diff_key($var, range(0, count($var) - 1))) != 0);
}

//----------------------------------------------------------------------- YAML -

/**
 * Prints a YAML associative array from a given PHP one.
 *
 * @param array $var 
 * @return void
 * @author Carlos
 */
function yaml_assoc_array($var)
{
  echo "{ ".preg_replace(
    array("~\\n~", "~,\s$~"),
    array(", ", null),
    sfYaml::dump($var)).
    " }".PHP_EOL;
}

//----------------------------------------------------------------- VALIDATION -

/**
 * Email Validator
 *
 * @param string $email 
 * @param string $check_domain 
 * @return void
 * @author Carlos
 */
function validate_email($email, $check_domain = true)
{
  if (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.
      '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
      '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $email))
  {
    if ($check_domain && function_exists('checkdnsrr'))
    {
      list (, $domain)  = explode('@', $email);
      if (checkdnsrr($domain.'.', 'MX') || checkdnsrr($domain.'.', 'A'))
      {
        return true;
      }
      return false;
    }
    return true;
  }
  return false;
}

//-------------------------------------------------------- REGULAR EXPRESSIONS -

if (!defined('REGEXP_IMG_TAG'))
  define('REGEXP_IMG_TAG', '~<\s*img [^\/>]*src\s*=\s*(["\'])(.*?)\1\s*\/>~');
if (!defined('REGEXP_HTML_ATRIBUTES'))
  define('REGEXP_HTML_ATRIBUTES', '~(\S+)=["\']?((?:.(?!["\']?\s+(?:\S+)=|[>"\']))+.)["\']?~');

function get_first_image($html)
{
  return count($imgs = get_all_images($html)) ? $imgs[0] : null;
}

function get_all_images($html)
{
  preg_match_all(REGEXP_IMG_TAG."i", $html, $matches);
  return (count($matches) && count($matches[0]) ? $matches[0] : array());
}

function remove_first_image($html)
{
  return str_ireplace(get_first_image($html), '', $html);
}

function remove_all_images($html)
{
  return str_ireplace(get_all_images($html), '', $html);
}

function extract_html_attributes($html)
{
  preg_match_all(REGEXP_HTML_ATRIBUTES."i", $html, $matches);
  $attributes = array();
  foreach ($matches[1] as $i => $name)
  {
    $attributes[$name] = $matches[2][$i];
  }
  return $attributes;
}

//---------------------------------------------------------------------- POSTS -

function related_posts($limit = 5, $template = 'fw_related_posts.html')
{
  global $post;
  
  if ($tags = wp_get_post_tags($post->ID))
  {
    $tagIDs = array();
    foreach ($tags as $tag)
      $tagIDs[] = $tag->term_id;
  
    $q = new WP_Query(array(
      'tag__in'      => $tagIDs,
      'post__not_in' => array($post->ID),
      'showposts'    => $limit,
      'orderby'      => 'date',
      'order'        => 'DESC',
      'caller_get_posts' => 1,
      ));
    
    $results = array();
    while ($q->have_posts())
    {
      $q->the_post();
      $results[] = array(
        'permalink' => get_permalink(),
        'title'     => get_the_title($post->ID),
        'title_attribute' => the_title_attribute('echo=0'),
        );
    }
    
    $t = Framework::Twig()->loadTemplate($template);
    $t->display(array('posts' => $results));
  }
}

//------------------------------------------------------------------------------